/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.pdf;

import org.ipti.br.tag.bean.dataManipulation.SelectionElement;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Unity;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 *
 * @author Felipe
 */
@Stateless
public class ConsentTermListPdf extends AbstractPdf {

    public void run(List<SelectionElement<Human, Boolean>> ctas, Unity logged, Unity selected, Campaign campaign)
            throws FileNotFoundException, DocumentException, IOException, Exception {
        
        this.init("LATC" + logged.getName() + "-" + campaign.getName() + "-" + selected.getName());
        
        this.generateScope(ctas,logged,selected,campaign);
        
        close();
        export();
    }
    
    public boolean generateScope(List<SelectionElement<Human, Boolean>> ctas, Unity logged, Unity selected, Campaign campaign) throws Exception { 
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();            
        Image image;
        image = Image.getInstance(externalContext.getRealPath("SLDI.jpg"));
        image.scaleAbsoluteWidth(140);
        image.scaleAbsoluteHeight(65);
        image.setAlignment( Element.ALIGN_CENTER);	
        image.setSpacingAfter(25);		
        Paragraph paragraph1 = new Paragraph("Colégio: "+ logged.getName() +".",super.getFontTextBold());	
        paragraph1.setAlignment( Element.ALIGN_JUSTIFIED);
        paragraph1.setSpacingAfter(6);

        Paragraph paragraphSpace = new Paragraph("",super.getFontTextBold());
        paragraphSpace.setSpacingAfter(7);
        Paragraph paragraph2 = new Paragraph("Campanha:"+ campaign.getName()+".",super.getFontTextBold());	
        paragraph2.setAlignment( Element.ALIGN_JUSTIFIED);
        paragraph2.setSpacingAfter(6);

        Paragraph paragraph333 = new Paragraph("Turma: "+ selected.getName()+".",super.getFontTextBold());	
        paragraph333.setAlignment( Element.ALIGN_JUSTIFIED);
        paragraph333.setSpacingAfter(10);

        Paragraph paragraph444 = new Paragraph("Lista de Alunos que tiveram o termo consentido.",super.getFontTextBold());	
        paragraph444.setAlignment( Element.ALIGN_CENTER);
        paragraph444.setSpacingAfter(15);

        PdfPTable table = new PdfPTable(1);        
        table.setWidthPercentage(100);
        PdfPCell cellName = new PdfPCell(new Paragraph("Nome",super.getFontTextBold()));
        table.addCell(cellName);

        for(SelectionElement<Human, Boolean> element : ctas){
            if(element.getStatus()){
                cellName = new PdfPCell(new Paragraph(element.getElement().getName(),super.getFontTextNORMAL()));
                table.addCell(cellName);
            }
        }

        super.getDocument().add(image);
        super.getDocument().add( paragraphSpace );
        super.getDocument().add(paragraph1);
        super.getDocument().add(paragraph2);
        super.getDocument().add(paragraph333);
        super.getDocument().add(paragraph444);
        super.getDocument().add(table);

        return true;
    }
}
